package com.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vehicle.bean.College;
import com.vehicle.bean.CollegeDetail;
import com.vehicle.form.CollegeForm;
import com.vehicle.mapper.CollegeDetailMapper;
import com.vehicle.mapper.CollegeMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle.mapper.UserLogMapper;
import com.vehicle.service.CollegeService;
import com.vehicle.util.LogUtil;
import com.vehicle.vo.CollegeListVO;
import com.vehicle.vo.CollegeOptionsVO;
import com.vehicle.vo.OptionDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-04-20
 */
@Service
public class CollegeServiceImpl extends ServiceImpl<CollegeMapper, College> implements CollegeService {

    @Autowired
    CollegeMapper collegeMapper;

    @Autowired
    CollegeDetailMapper collegeDetailMapper;

    @Autowired
    UserLogMapper userLogMapper;

    //添加学院信息

    @Override
    public boolean insertCollege(String ip,Integer id,CollegeForm collegeForm) {
        College college = new College();
        college.setCollegeName(collegeForm.getCollegeName());
        college.setUniverId(collegeForm.getUniverId());
        int b = collegeMapper.insert(college);
        if(b==1){
            userLogMapper.insert(LogUtil.insertAdminInsert(id,"添加学院"+collegeForm.getCollegeName(),ip,"成功"));
            return true;
        }
        userLogMapper.insert(LogUtil.insertAdminInsert(id,"添加学院"+collegeForm.getCollegeName(),ip,"失败"));
        return false;
    }

    //根据学院id获取学院名称
    @Override
    public String getNameById(Integer collegeId) {
        System.out.println("没进来吗");
        return collegeMapper.getNameById(collegeId);
    }

    //根据学院id获取学院所属学校id
    @Override
    public Integer getUniverId(Integer collegeId) {
        return collegeMapper.getUniverIdById(collegeId);
    }

    @Override
    public CollegeListVO getAllCollegeList(Integer page, Integer size) {
        Page<College> pageInfo = new Page<>(page,size);
        Page<College> collegePage = collegeMapper.selectPage(pageInfo,null);
        CollegeListVO collegeListVO = new CollegeListVO();
        collegeListVO.setSize(collegePage.getSize());
        collegeListVO.setTotal(collegePage.getTotal());
        collegeListVO.setContent(collegePage.getRecords());
        return collegeListVO;
    }

    @Override
    public CollegeListVO getAllListByUniverId(Integer univerId, Integer page, Integer size) {
        Page<College> pageInfo = new Page<>(page,size);
        QueryWrapper<College> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("univer_id",univerId);
        Page<College> collegePage = collegeMapper.selectPage(pageInfo,queryWrapper);
        CollegeListVO collegeListVO = new CollegeListVO();
        collegeListVO.setSize(collegePage.getSize());
        collegeListVO.setTotal(collegePage.getTotal());
        collegeListVO.setContent(collegePage.getRecords());
        return collegeListVO;
    }

    //根据学院id修改学院信息
    @Override
    public Boolean updateTheCollage(String ip,Integer id,College college) {
        UpdateWrapper<College> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("college_id",college.getCollegeId());
        updateWrapper.set("college_name",college.getCollegeName());
        updateWrapper.set("univer_id",college.getUniverId());
        int b = collegeMapper.update(null,updateWrapper);
        if(b==1){
            userLogMapper.insert(LogUtil.insertAdminUpdate(id,"修改学院ID"+college.getCollegeId(),ip,"成功"));
            return true;
        }
        userLogMapper.insert(LogUtil.insertAdminUpdate(id,"修改学院"+college.getCollegeId(),ip,"失败"));
        return false;
    }

    //根据学院id删除学院信息
    @Override
    public Boolean deleteCollegeById(String ip,Integer id,Integer collegeId) {
        int c = collegeMapper.deleteById(collegeId);
        if(c==1){
            userLogMapper.insert(LogUtil.insertAdminDelete(id,"删除学院"+collegeId,ip,"成功"));
            return true;
        }
        userLogMapper.insert(LogUtil.insertAdminDelete(id,"删除学院"+collegeId,ip,"失败"));
        return false;
    }

    @Override
    public List<CollegeOptionsVO> getCollegeOp() {
        List<CollegeOptionsVO> lists = new ArrayList<>();
        QueryWrapper<College> collegeQueryWrapper = new QueryWrapper<>();
        collegeQueryWrapper.eq("univer_id",1);
        List<College> colleges = collegeMapper.selectList(collegeQueryWrapper);
        for (College c:colleges) {
            CollegeOptionsVO collegeOptionsVO = new CollegeOptionsVO();
            collegeOptionsVO.setLabel(c.getCollegeName());
            collegeOptionsVO.setValue(c.getCollegeName());
            QueryWrapper<CollegeDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("college_id",c.getCollegeId());
            List<CollegeDetail> collegeDetails = collegeDetailMapper.selectList(queryWrapper);
            List<OptionDetail> optionDetails = new ArrayList<>();
            for (CollegeDetail cd:collegeDetails) {
                OptionDetail optionDetail = new OptionDetail();
                optionDetail.setLabel(cd.getDetail());
                optionDetail.setValue(cd.getDetail());
                optionDetails.add(optionDetail);
            }
            collegeOptionsVO.setChildren(optionDetails);
            lists.add(collegeOptionsVO);
        }
        return lists;
    }
}
